﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

//using SIPO.Library.DomainObject.Administration;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Report;
using System.Data;
using Microsoft.Reporting.WebForms;
using SIPO.Web.Support;

namespace SIPO.Web.lists.Report
{
    public partial class rptInvoicelist : BasePage
    {
        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {
                recDebtorRepo debtor = new recDebtorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recInvoiceRepo recInvoiceDal = new recInvoiceRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                string parameters = string.Empty;
                List<recDebtor> ldebtor = debtor.GetList();

                ddlDebitur.Items.Clear();
                if (ldebtor.Count > 0)
                {
                    ldebtor.ForEach(delegate(recDebtor ldebtors)
                    {
                        ddlDebitur.Items.Add(new ListItem(ldebtors.DebtorID + " - " + ldebtors.DebtorName, ldebtors.DebtorID.ToString()));
                    });
                }
                ddlDebitur.Items.Insert(0, new ListItem("-- Perusahaan --", string.Empty));

                if (Context.Request.Params["periodeAwal"] != null || Context.Request.Params["periodeAkhir"] != null || Context.Request.Params["debtorID"] != null)
                {
                    DateTime? dPeriodeAwal = Context.Request.Params["periodeAwal"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["periodeAwal"], Helper.Culture.Indonesian);
                    DateTime? dPeriodeAkhir = Context.Request.Params["periodeAkhir"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["periodeAkhir"], Helper.Culture.Indonesian);
                    string debtorID = Context.Request.Params["debtorID"] == null ? string.Empty : Context.Request.Params["debtorID"];

                    //konversi tanggal
                    string periodeAwal = string.Format("{0:yyyyMMdd}", dPeriodeAwal);
                    string periodeAkhir = string.Format("{0:yyyyMMdd}", dPeriodeAkhir);

                    // filter debtorID
                    if (!debtorID.Equals(string.Empty))
                    {
                        parameters += "where recDebtor.DebtorID = ISNULL ('" + debtorID + "', recDebtor.DebtorID)";
                    }

                    //filter invoice date
                    if (!debtorID.Equals(string.Empty))
                    {
                        if (!periodeAwal.Equals(string.Empty) && !periodeAkhir.Equals(string.Empty))
                            parameters += " AND recInvoice.InvoiceDate BETWEEN ISNULL ('" + periodeAwal + "', recInvoice.InvoiceDate) AND ISNULL ('" + periodeAkhir + "', recInvoice.InvoiceDate)";
                    }
                    else if (!periodeAwal.Equals(string.Empty) && !periodeAkhir.Equals(string.Empty))
                    {
                        parameters += "WHERE recInvoice.InvoiceDate BETWEEN ISNULL ('" + periodeAwal + "', recInvoice.InvoiceDate) AND ISNULL ('" + periodeAkhir + "', recInvoice.InvoiceDate)";
                    }

                    try
                    {
                        DataTable dt = recInvoiceDal.GetListReport(parameters);

                        rsViewer.Reset();
                        rsViewer.LocalReport.ReportPath = Server.MapPath("~/reportviewer/Transaction/rptInvoice1.rdlc");
                        ReportDataSource rds = new ReportDataSource();
                        rds.Name = "DataSet1";
                        rds.Value = dt;

                        rsViewer.LocalReport.DataSources.Clear();
                        rsViewer.LocalReport.DataSources.Add(rds);
                        rsViewer.LocalReport.Refresh();
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                }
            }
        }
    }
}